package controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.NhaCC;
import model.NhanVien;

public class NhanVienController {
	public static NhanVien getNhanVien(int manv) {
		Connection conn = DBConection.MYSQL;
		if (conn == null)
			return null;
		try {

			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM nhanvien WHERE manv=" + manv + ";";
			ResultSet rs = stmt.executeQuery(sql);
			NhanVien nv;
			if (rs.next()) {
				nv = new NhanVien();
				nv.setMaNV(rs.getInt("manv"));
				nv.setTenNV(rs.getString("tennv"));
				nv.setChucVu(rs.getString("chucvu"));
				nv.setDiaChi(rs.getString("diachi"));
				nv.setSdt(rs.getString("sdt"));
				nv.setLuongCB(rs.getInt("luongcb"));

				return nv;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}

	public static List<NhanVien> getDSNhanVien() {
		Connection conn = DBConection.MYSQL;
		if (conn == null)
			return null;
		try {

			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM nhanvien";
			ResultSet rs = stmt.executeQuery(sql);
			List<NhanVien> list = new ArrayList<>();
			NhanVien nv;
			while (rs.next()) {
				nv = new NhanVien();
				nv.setMaNV(rs.getInt("manv"));
				nv.setTenNV(rs.getString("tennv"));
				nv.setChucVu(rs.getString("chucvu"));
				nv.setDiaChi(rs.getString("diachi"));
				nv.setSdt(rs.getString("sdt"));
				nv.setLuongCB(rs.getInt("luongcb"));
				list.add(nv);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}

	public static void insertNhanVien(NhanVien nv) {
		try {
			Statement sta = DBConection.MYSQL.createStatement();
			String query = "INSERT INTO nhanvien (manv,tennv,chuvu,diachi,sdt,luongcb) VALUES (";
			query += "'" + nv.getMaNV() + "',";
			query += "'" + nv.getTenNV() + "',";
			query += "'" + nv.getChucVu() + "',";
			query += "'" + nv.getDiaChi() + "',";
			query += "'" + nv.getSdt() + "')";
			query += "'" + nv.getLuongCB() + "')";
			System.out.println(query);
			sta.execute(query);
			return;

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public static void updateNhanVien(NhanVien nv) {
		try {
			Statement st = DBConection.MYSQL.createStatement();
			String query = "UPDATE nhanvien SET " + "tennv=" + "'"
					+ nv.getTenNV() + "'" + "," + "chucvu= " + "'"
					+ nv.getChucVu() + "'" + "," + "diachi=" + "'"
					+ nv.getDiaChi() + "'" + "," + "sdt= " + "'" + nv.getSdt()
					+ "'" + "," + "luongcb= " + "'"  + nv.getLuongCB()
					+ "'" + "," + " WHERE manv =" + nv.getMaNV();
			System.out.println(query);
			st.execute(query);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void deleteNhanVien(int manv) {
		try {
			Statement st = DBConection.MYSQL.createStatement();
			String query = "DELETE FROM nhanvien WHERE manv = " + manv;
			st.execute(query);
			return;
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		NhanVienController u = new NhanVienController();
		System.out.println(u.getDSNhanVien());
//		u.updateNhanVien(new NhanVien(3, "Nguyen An", "nhân viên", "thủ đức",
//				"090374379", 1800000));

	}
}